<?php
/**
 * Created by PhpStorm OF Administrator.
 * User: YUYI
 * Date: 2018/7/9 0009
 * Time: 下午 5:46
 * Administrator
 */

namespace app\common\hook;


use app\common\util\ReturnMsgUtil;
use think\facade\Request;
use think\facade\Validate;
use think\Hook;

class AppInit extends Hook
{
    use ReturnMsgUtil;
    public function run()
    {
        $this->validate();
    }

    /**
     * 自动获取验证器，并验证规则
     */
    protected function validate()
    {
        $arg = Request::instance();
        $valiClass = 'app\\'.$arg->module().'\\validate\\'.$arg->controller().'Validate';
        if (class_exists($valiClass)){          #存在验证类
            /**
             * @var $vali Validate
             */
            $vali = new $valiClass();
            if ($vali->hasScene($arg->action(true))) {  #存在验证场景
                if (!$vali->scene($arg->action(true))->check(input())) {
                    exit(self::errorInfo($vali->getError()));
                }
            }
        }
    }
}